//
//  KKDraftController.m
//  DoMi
//
//  Created by 康凯 on 2016/11/16.
//  Copyright © 2016年 kk. All rights reserved.
//

#import "KKDraftController.h"
#import "KKDraftCell.h"
#import "KKMyHttp.h"
#import "KKCollectDocumentM.h"
#import "KKPublishArticleVC.h"
#import "KKCollectDocumentCell.h"
@interface KKDraftController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong,nonatomic)NSArray *datas;
@end

@implementation KKDraftController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"草稿箱";
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    [self.tableView registerNib:[UINib nibWithNibName:@"KKCollectDocumentCell" bundle:nil] forCellReuseIdentifier:@"KKCollectDocumentCell"];
    
    [self getDraftList];
}

#pragma mark tableview代理
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    KKCollectDocumentCell *draft=(KKCollectDocumentCell *)cell;
    [draft initDraftData:self.datas[indexPath.section]];
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.datas.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [tableView dequeueReusableCellWithIdentifier:@"KKCollectDocumentCell"];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 8;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(section==0){
        return 8;
    }
    return 0.1;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    KKCollectDocumentM *draft= self.datas[indexPath.section];
    KKPublishArticleVC *detail=[[KKPublishArticleVC alloc] init];
    detail.docId=draft.articleid;
    detail.refreshListBlock=^(){
        [self getDraftList];
    };
    [self.navigationController pushViewController:detail animated:YES];
}

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

//定义编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

//进入编辑模式，按下出现的编辑按钮后
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    KKCollectDocumentM *draft= self.datas[indexPath.section];
    [self delArticleDraft:draft.articleid];
    
}

//修改编辑按钮文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

#pragma mark 接口--草稿箱列表
-(void)getDraftList{
    [self showDialog];
    [KKMyHttp getDraftBoxList:@"1" success:^(id data) {
        self.datas=data;
        [self.tableView reloadData];
        [self dismissDialog];
    } failure:^(NSError *error) {
        [self dismissDialog];
    }];
}
#pragma mark 接口--删除草稿箱文章
-(void)delArticleDraft:(NSString *)articleId{
    [KKMyHttp delDraftBox:articleId success:^(id data) {
        [self toast:@"删除成功"];
        [self getDraftList];
    } failure:^(NSError *error) {
    }];
}
@end
